package net.neiquanlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.neiquanlib.R;

import org.haitao.common.utils.AppLog;


/**
 * 作者 : hjb
 * 时间 : 2016/6/24.
 */
public class HorizontalTextItem extends LinearLayout {

    private int left_img_visible;
    private static final int DEFAULT_TAG_BACKGROUND_COLOR = 0xff545454;
    private int item_text_color;
    private float mDensity;
    private static final int DEFAULT_CORNER_DISTANCE = 15;
    private static final int DEFAULT_RIGHT_SIZE = 15;
    private static final int DEFAULT_IMAGE_SIZE = 28;
    private String item_text;
    private float item_text_size;
    private int left_img;
    private Context mContext;
    private View mView;
    private ImageView mLeftImg;
    private TextView mText;
    private int left_img_size;
    private String right_text;
    private int right_text_color;
    private float right_text_size;
    private TextView mTextRight;

    public HorizontalTextItem(Context context) {
        this(context, null);
    }

    public HorizontalTextItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalTextItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mDensity = context.getResources().getDisplayMetrics().density;
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.HorizontalTextItem, defStyleAttr,0);
        left_img_visible = a.getInteger(R.styleable.HorizontalTextItem_left_img_visible, 0);
        item_text_color = a.getColor(R.styleable.HorizontalTextItem_item_text_color, DEFAULT_TAG_BACKGROUND_COLOR);
        item_text = a.getString(R.styleable.HorizontalTextItem_item_text);
        right_text = a.getString(R.styleable.HorizontalTextItem_right_text);
        right_text_color = a.getColor(R.styleable.HorizontalTextItem_right_text_color, DEFAULT_TAG_BACKGROUND_COLOR);
        AppLog.e("--------" + R.styleable.HorizontalTextItem_item_text_size);
        right_text_size = a.getDimensionPixelSize(R.styleable.HorizontalTextItem_right_text_size, dip2px(DEFAULT_RIGHT_SIZE));
        item_text_size = a.getDimensionPixelSize(R.styleable.HorizontalTextItem_item_text_size, dip2px(DEFAULT_RIGHT_SIZE));
        left_img_size = (int) a.getDimensionPixelSize(R.styleable.HorizontalTextItem_left_img_size, dip2px(DEFAULT_IMAGE_SIZE));
        left_img = a.getResourceId(R.styleable.HorizontalTextItem_left_img, 0);
        a.recycle();
        init();
    }

    private void init() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.item_horizontal_text, null);
        mLeftImg = (ImageView) mView.findViewById(R.id.left_Img);
        mText = (TextView) mView.findViewById(R.id.text);
        mTextRight = (TextView) mView.findViewById(R.id.textRight);
        LayoutParams layoutParams = new LayoutParams(left_img_size, left_img_size);
        mLeftImg.setLayoutParams(layoutParams);
        if (left_img != 0) {
            mLeftImg.setImageResource(left_img);
        }
        mLeftImg.setVisibility(left_img_visible);
        if (!TextUtils.isEmpty(item_text)) {
            mText.setText(item_text);
        }
        mTextRight.setText(right_text);
        mTextRight.setTextColor(right_text_color);
        mTextRight.setTextSize(px2dip(right_text_size));
        mText.setTextColor(item_text_color);
        mText.setTextSize(px2dip(item_text_size));
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(mView, lp);
    }

    /**
     * 条目左边图片资源
     *
     * @param ids
     */
    public void setLeftImg(Integer ids) {
        mLeftImg.setImageResource(ids);
    }

    /**
     * 条目左边图片是否可见
     *
     * @param visibility
     */
    public void setLeftImgVisible(int visibility) {
        mLeftImg.setVisibility(visibility);
    }

    /**
     * 条目左边图片的大小
     *
     * @param size
     */
    public void setLeftImgSize(int size) {
        LayoutParams layoutParams = new LayoutParams(dip2px(size), dip2px(size));
        mLeftImg.setLayoutParams(layoutParams);
    }

    /**
     * 条目的文本内容
     *
     * @param str
     */
    public void setItemText(String str) {
        mText.setText(str);
    }

    /**
     * 条目的文本的颜色
     *
     * @param color
     */
    public void setItemTextColor(int color) {
        mText.setTextColor(color);
    }

    /**
     * 条目文本的大小
     *
     * @param size
     */
    public void setItemTextSize(int size) {
        mText.setTextSize(size);
    }


    /**
     * 条目右边文本内容
     *
     * @param str
     */
    public void setRightText(String str) {
        mTextRight.setText(str);
    }

    /**
     * 条目右边文本的颜色
     *
     * @param color
     */
    public void setRightTextColor(int color) {
        mTextRight.setTextColor(color);
    }

    /**
     * 条目右边文本的大小
     *
     * @param size
     */
    public void setRightTextSize(int size) {
        mTextRight.setTextSize(size);
    }

    private int dip2px(int dip) {
        return (int) (mDensity * dip + 0.5f);
    }

    public int px2dip(float pxValue) {
        return (int) (pxValue / mDensity + 0.5f);
    }
}
